<?php

include("pubmed.class.php");
include("pubmeddata.class.php");

class BAL
{
    public $service;
    public $params;

    private $totalRecords;

    function  __construct()
    {
        $this->service = new PubMed();
    }

    public function getSearchResults($queryString)
    {
        $keyword = $this->getSearchKeyword($queryString);
        $start_from = $this->getStartRecord();
        $num_records = $this->getSearchCount();
        
        $id_array = array();
        $results_array = array();

        //search the term using run_eSearch service
        $records = $this->service->DocSearch(urlencode($keyword), $start_from, $num_records);

        if($records->Count)
        {
            $this->totalRecords = $records->Count;
        }

        if($records)
        {
            foreach($records->IdList->Id as $doc_id)
            {
                $id_array[] = $doc_id;
            }

            //fetch all docs in batch by passing IDs
            $docs = $this->service->DocFetch($id_array);

            if($docs)
            {
                foreach ($docs->PubmedArticle as $article)
                {
                    $doc = new PubMedData();

                    $doc->id = $article->MedlineCitation->PMID;
                    $doc->url = $this->service->docHost
                            . $article->MedlineCitation->PMID;
                    $doc->dateCreated = $article->MedlineCitation->DateCreated->Year
                            . "-" . $article->MedlineCitation->DateCreated->Month
                            . "-" . $article->MedlineCitation->DateCreated->Day;
                    $doc->journal = $article->MedlineCitation->Article->Journal->Title;
                    $doc->pubDate = $article->MedlineCitation->Article->Journal->JournalIssue->PubDate->Month
                            . " " . $article->MedlineCitation->Article->Journal->JournalIssue->PubDate->Year;
                    $doc->journalIssue = "Volume " . $article->MedlineCitation->Article->Journal->JournalIssue->Volume
                            . "- Issue " . $article->MedlineCitation->Article->Journal->JournalIssue->Issue;
                    $doc->authorList = "";

                    foreach($article->MedlineCitation->Article->AuthorList->Author as $author)
                    {
                        $doc->authorList .= $author->ForeName . " " . $author->LastName . ", ";
                    }

                    $doc->authorList = rtrim($doc->authorList, ", ");

                    $doc->articleTitle = $article->MedlineCitation->Article->ArticleTitle;
                    $doc->abstractText = $article->MedlineCitation->Article->Abstract->AbstractText;
                    $doc->affiliation = $article->MedlineCitation->Article->Affiliation;
                    $doc->language = $article->MedlineCitation->Article->Language;

                    $results_array[] = $doc;
                }
            }
        }

        return $results_array;
    }

    public function getTotalRecords()
    {
        return $this->totalRecords;
    }

    public function getSearchKeyword($queryString)
    {
        $searchKeyword = "";

        $this->params = $this->getQueryStringValues($queryString);

        if($this->params)
        {
            if(isset ($this->params["searchValue"]))
            {
                if(is_array($this->params["searchValue"]) && count($this->params["searchValue"]) > 0)
                {
                    $searchKeyword = $this->params["searchValue"][0];
                }
                else
                {
                    $searchKeyword = $this->params["searchValue"];
                }
            }
        }

        return $searchKeyword;
    }

    public function getSearchCount()
    {
        $searchCount = 10;

        if($this->params)
        {
            if(isset ($this->params["numberOfRows"]))
            {
                if(is_array($this->params["numberOfRows"]) && count($this->params["numberOfRows"]) > 0)
                {
                    $searchCount = $this->params["numberOfRows"][0];
                }
                else
                {
                    $searchCount = $this->params["numberOfRows"];
                }
            }
        }

        return $searchCount;
    }

    public function getStartRecord()
    {
        $startRecord = 1;

        if($this->params)
        {
            if(isset ($this->params["startRow"]))
            {
                if(is_array($this->params["startRow"]) && count($this->params["startRow"]) > 0)
                {
                    $startRecord = $this->params["startRow"][0];
                }
                else
                {
                    $startRecord = $this->params["startRow"];
                }
            }
        }

        return $startRecord;
    }

    public function getQueryStringValues($query_string)
    {
        $query  = explode('&', $query_string);
        $params = array();

        foreach( $query as $param )
        {
            list($name, $value) = explode('=', $param);
            $params[urldecode($name)][] = urldecode($value);
        }

        return $params;
    }
}
?>
